<template>
    <div>
        <div style="display: inline-flex;margin-top: 20px">
            <el-input
                    v-model="keyword"
                    :disabled="disabled"
                    :placeholder="placeholder"
                    prefix-icon="el-icon-search"
                    style="width: 350px;margin-right: 10px"
                    clearable
                    @clear="search"
                    @keydown.enter.native="search"></el-input>
            <el-button
                    :disabled="disabled"
                    icon="el-icon-search"
                    type="primary"
                    @click="search">
                搜索
            </el-button>
        </div>

        <div>
            <h3>心理测评</h3>
            <!--                新增-->
            <el-button type="text" @click="dialogTableVisible = true" size="small">新增</el-button>
            <el-dialog title="新增" :visible.sync="dialogTableVisible" >
                <el-form :model="form">
                    <el-form-item label="序号" :label-width="formLabelWidth">
                        <el-input v-model="form.id" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="职称" :label-width="formLabelWidth">
                        <el-input v-model="form.title" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="头像" :label-width="formLabelWidth" style="text-align: left">
                        <el-upload name="file"
                                   v-model="form.img"
                                   :on-success="uploadImg"
                                   action="http://localhost:8080/psychologyKnowledge/upload" list-type="picture-card" :auto-upload="true">
                            <i slot="default" class="el-icon-plus"></i>
                            <div slot="file" slot-scope="{file}">
                                <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
                                <span class="el-upload-list__item-actions">
                            <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                                 <i class="el-icon-zoom-in"></i>
                            </span>
                            <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                                 <i class="el-icon-download"></i>
                            </span>
                                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                                <i class="el-icon-delete"></i>
                                </span>
                            </span>
                            </div>
                        </el-upload>
                        <el-dialog :visible.sync="dialogVisible">
                            <img width="100%" :src="dialogImageUrl" alt="">
                        </el-dialog>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogTableVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <!-- 修改 -->
            <el-button type="text" @click="dialogFormVisible2 = true" size="small">修改</el-button>
            <el-dialog title="修改文章" :visible.sync="dialogFormVisible2" >
                <el-form :model="form">
                    <el-form-item label="序号" :label-width="formLabelWidth">
                        <el-input v-model="form.id" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="职称" :label-width="formLabelWidth">
                        <el-input v-model="form.title" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="头像" :label-width="formLabelWidth" style="text-align: left">
                        <el-upload name="file"
                                   v-model="form.psychologyimg"
                                   :on-success="img"
                                   action="http://localhost:8080/psychologyKnowledge/upload" list-type="picture-card" :auto-upload="true">
                            <i slot="default" class="el-icon-plus"></i>
                            <div slot="file" slot-scope="{file}">
                                <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
                                <span class="el-upload-list__item-actions">
                            <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                                 <i class="el-icon-zoom-in"></i>
                            </span>
                            <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                                 <i class="el-icon-download"></i>
                            </span>
                                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                                <i class="el-icon-delete"></i>
                                </span>
                            </span>
                            </div>
                        </el-upload>
                        <el-dialog :visible.sync="dialogVisible">
                            <img width="100%" :src="dialogImageUrl" alt="">
                        </el-dialog>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                    <el-button type="primary" :plain="true" @click="Update">保存</el-button>
                </div>
            </el-dialog>
            <el-table
                    :data="tableData"
                    border
                    style="width: 100%">
                <el-table-column
                        prop="id"
                        label="序号"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="img"
                        label="图片"
                        width="380">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="标题"
                        width="380">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="400">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="删除" placement="top">
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteVisible(scope.$index)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </div>



    </div>
</template>

<script>
    export default {
        name: "SearchHeartTest",
        props: {
            disabled: {
                type: Boolean,
                default: false
            },
            placeholder: {
                type: String,
                default: '请输入内容'
            },
        },
        data() {
            return {
                tableData: [{
                    id: '',
                    img:'',
                    title: '',
                }],
                form:[{
                    id: '',
                    img:'',
                    title: '',
                }],
                searchText: '',
                dialogTableVisible: false,
                dialogFormVisible: false,
                dialogFormVisible2:false,
                formLabelWidth: '120px',
                keyword:'',
                //    照片墙
                dialogImageUrl: '',
                dialogVisible: false,
                disabled1: false,
            }
        },
        methods:{
            search(){
                this.axios.get("/heartTest/selectLike",
                    {
                        params:{
                            title:this.keyword
                        }
                    })
                    .then(resp=>{
                        console.log(resp)
                        const r = resp.data;
                        this.tableData = r.data;
                        // setTimeout(()=>{this.refresh()},500)
                    });
            },
            refresh(){
                this.axios.get("/heartTest/selectAll")
                    .then(resp=>{
                        const r = resp.data;
                        this.tableData = r.data;
                    });
            },
            add(){
                this.dialogFormVisible = true;
                //提交请求
                this.axios.post("/heartTest/insert", {
                    id: this.form.id,
                    title: this.form.title,
                    img: this.form.img,
                }).then((resp)=>{
                    const r = resp.data;
                    console.log(r);
                    this.$message.success("添加成功!")
                })
                //关闭
                this.dialogFormVisible = false;
                // setTimeout(()=>{this.refresh()},500)
                location.reload();

            },
            Update(){
                this.dialogFormVisible = true;
                this.axios.post("/heartTest/update", {
                    id: this.form.id,
                    title: this.form.title,
                    img: this.form.img,
                }).then((resp)=>{
                    const r = resp.data;
                    console.log(r);
                    this.$message.success("修改成功!")
                })
                //关闭
                this.dialogFormVisible2 = false;
                setTimeout(()=>{this.refresh()},500)
            },
            //        删除
            deleteVisible(index){
                this.form.id = this.tableData[index].id
                //提交请求
                this.axios.delete("/heartTest/delete",{
                    params:{
                        id: this.form.id
                    }
                }).then((resp)=>{
                    const r = resp.data;
                    console.log(r);
                    this.$message.success("删除成功!")
                    setTimeout(()=>{this.refresh()},500)
                })
                // location.reload();
            },
            //        照片墙
            handleRemove(file) {
                console.log(file);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleDownload(file) {
                console.log(file);
            },
            uploadImg(resp) {
                console.log(resp.data);
                this.form.img = resp.data
            },

        },

        created() {
            this.refresh()
        }
    }
</script>

<style scoped>

</style>